<?php namespace App\Http\Controllers;

use App\Answers;
use App\Banner;
use App\DakaContent;
use App\Http\Services\FileManager;
use App\Page;
use App\Plan;
use App\PlanJoin;
use App\Profession;
use App\Topic;
use App\Answer;
use App\TopicAnswer;
use App\TopicAnswerWrite;
use App\Classify;
use App\User;
use App\UserInfo;
use App\VisitLog;
use App\Admin;
use App\HomeImg;
use Illuminate\Contracts\Auth\Guard;
use Auth;
use DB;
use Illuminate\Http\Request;

class AdminController extends Controller
{
    public function login(){
        return view('admin.login');
    }


    public function userList(Request $request){
        if(Auth::guard("admin")->guest())
            return response()->json(['level'=>'error','message' => '请先登录！']);

        $users = User::orderBy("created_at","desc")->paginate(50);

        foreach($users as $user){
            /** @var User $user */
            $user->userInfo;
        }

        $user = Auth::guard('admin')->user();

        $pp = $user->username;

        $isAdmin = Admin::where('username',$pp)->first();

        return view('admin.user-list',["users"=>$users,'isAdmin' => $isAdmin]);
    }

    public function classifyList(Request $request){

        if(Auth::guard('admin')->guest())
            return redirect(route('Admin.Home.login'));

        $classify = Classify::orderBy('sort','asc')->orderBy('created_at','desc')->paginate(20);

        return view('admin.classify-list',['classify'=>$classify]);
    }

    public function topicList(Request $request){

        if(Auth::guard('admin')->guest())
            return redirect(route('Admin.Home.login'));

        $topic = Topic::orderBy('created_at','desc')->paginate(30);
        if($topic){
            foreach($topic as $t){
                $classify = Classify::whereId($t->classify_id)->first();
                if($classify)
                    $t->classify_name = $classify->name;
                $t->answers = Answer::whereTaId($t->id)->get();
                $t->select_answers = Answer::whereTaId($t->id)->whereSelectAnswer(1)->get();
            }
        }

        return view('admin.topic-list',['topic'=>$topic]);
    }

    public function topicSet(Request $request){
        if(Auth::guard('admin')->guest())
            return redirect(route('Admin.Home.login'));

        $id = !is_null($request->get('id'))?$request->get('id'):0;

        $sysTopic = Topic::whereId($id)->first();

        $classify = Classify::get();

        return view('admin.topic-set',['sysTopic' => $sysTopic,'classify' => $classify]);
    }

    public function answerList(Request $request){
        if(Auth::guard('admin')->guest())
            return redirect(route('Admin.Home.login'));

        $id = !is_null($request->get('id'))?$request->get('id'):0;

        $answers = Answer::whereTaId($id)->orderBy('answer_num','asc')->get();

        return view('admin.answer-list',['answers' => $answers,'id' => $id]);

    }

    public function answerSet(Request $request){
        if(Auth::guard('admin')->guest())
            return redirect(route('Admin.Home.login'));

        $id = !is_null($request->get('id'))?$request->get('id'):0;

        $aid = !is_null($request->get('aid'))?$request->get('aid'):0;

        $sysAnswer = Answer::whereId($aid)->whereTaId($id)->first();

        return view('admin.answer-set',['sysAnswer' => $sysAnswer,'id' => $id]);
    }

    public function topicAnswerList(Request $request){
        if(Auth::guard('admin')->guest())
            return redirect(route('Admin.Home.login'));

        $topicAnswer = TopicAnswer::orderBy('score','desc')->orderBy('created_at','desc')->paginate(30);

        return view('admin.topic_answer-list',['topicAnswer' => $topicAnswer]);
    }

    public function topicAnswerWriteList(Request $request){
        if(Auth::guard('admin')->guest())
            return redirect(route('Admin.Home.login'));

        $id = !is_null($request->get('id'))?$request->get('id'):0;

        $topicAnswerWrite = TopicAnswerWrite::whereTacId($id)->orderBy('created_at','desc')->paginate(30);
        if(count($topicAnswerWrite) > 0){
            foreach($topicAnswerWrite as $topicAnswer){
                $topic = Topic::whereId($topicAnswer->t_id)->first();
                if($topic)
                    $topicAnswer->t_id = $topic->topic;
            }
        }

        return view('admin.topic_answer_write-list',['topicAnswerWrite' => $topicAnswerWrite,'id' => $id]);
    }


    public function changePwd(){
        if(Auth::guard('admin')->guest())
            return redirect(route('Admin.Home.login'));

        $user = Auth::guard('admin')->user();

        $pp = $user->username;

        $isAdmin = Admin::where('username',$pp)->first();
        return view('admin.change-psw',['isAdmin' => $isAdmin]);
    }
}